<template>
  <view>
    <view class="goods-item">
      <!-- 商品左侧图片区域 -->
      <view class="goods-item-left">
        <radio :checked="goods.goods_state"  @click="radioClickHandler" color="#C00000" v-if="showRadio"></radio>
        <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="goods-item-right">
        <!-- 商品标题 -->
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info-box">
          <!-- 商品价格 -->
          <view class="goods-price">￥{{tofixed(goods.goods_price)}}</view>
           <!-- 商品数量 -->
            <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    props:{
      goods:{
        type: Object,
        default: {}
      },
      // 是否展示图片左侧的 radio
      showRadio: {
        type: Boolean,
        // 如果外界没有指定 show-radio 属性的值，则默认不展示 radio 组件
        default: false,
      },
      // 是否展示价格右侧的 NumberBox 组件
      showNum: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        defaultPic: 'https://img1.baidu.com/it/u=1272997171,24557948&fm=253&fmt=auto&app=138&f=JPEG?w=280&h=280'
      };
    },
    methods:{
      // 把数字处理为带两位小数点的数字
        tofixed(num) {
          return Number(num).toFixed(2)
        },
        radioClickHandler(){
          this.$emit('radio-change',{
            goods_id: this.goods.goods_id,
            // 商品最新的勾选状态
            goods_state: !this.goods.goods_state
          })
        },
        // 商品数量发生改变
        numChangeHandler(val){
          this.$emit('num-change',{
            goods_id:this.goods.goods_id,
            goods_count:+val
          })
        }
    }
  }
</script>

<style lang="scss">
.goods-item {
 // 让 goods-item 项占满整个屏幕的宽度
  width: 750rpx;
  // 设置盒模型为 border-box
  box-sizing: border-box;
  display: flex;
  padding: 20rpx 10rpx;
  border-bottom: 2rpx solid #f0f0f0;
  .goods-item-left {
    margin-right: 10rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-pic {
      width: 200rpx;
      height: 200rpx;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: space-between;
    .goods-name {
      font-size: 26rpx;
    }
   .goods-info-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .goods-price {
      font-size: 32rpx;
      color: #c00000;
    }
  }
}
</style>